<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/ZendSkeletonApplication for the canonical source repository
 * @copyright Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Crm\Controller;

use Zend\View\Model\ViewModel;

class CrmuserController extends AbstractController
{
    public function indexAction()
    {
        return new ViewModel();
    }
    public function addUserAction()
    {
        $organizationList = array();
        $organizationList = $this->getOrganizationTable()->findAll();
        $groupList = array();
        $groupList = $this->getUserGroupTable()->findAll();
        
        //$data = $this->request->getQuery();
        $crmUser=array(
            'user_id'=>'',
            'user_account'=>'',
            'user_password'=>'',
            'user_password_old'=>'',
            'user_name'=>'',
            'user_position'=>'',
            'user_group_id'=>'',
            'org_id'=>'',
        );
//         if(isset($data['id'])){
//             $crmUserTable=$this->getCrmUserTable();
//             $crmUser = $crmUserTable->getCrmUserById($data['id']);
//             $crmUser['user_password_old']=$crmUser['user_password'];
//         }
//         var_dump($data);
//         die();
        return new ViewModel(array(
//            'type'=>$data['type'],
            'user'=>$crmUser,
            'organization'=>$organizationList,
            'groupList'=>$groupList,
        ));
    }
    public function editUserAction()
    {
        $organizationList = array();
        $organizationList = $this->getOrganizationTable()->findAll();
        $groupList = array();
        $groupList = $this->getUserGroupTable()->findAll();
        
        $data = $this->request->getQuery();
        $crmUser=array(
            'user_id'=>'',
            'user_account'=>'',
            'user_password'=>'',
            'user_password_old'=>'',
            'user_name'=>'',
            'user_position'=>'',
            'user_group_id'=>'',
            'org_id'=>'',
        );
        if(isset($data['id'])){
            $crmUserTable=$this->getCrmUserTable();
            $crmUser = $crmUserTable->getCrmUserById($data['id']);
            $crmUser['user_password_old']=$crmUser['user_password'];
        }
//         var_dump($data);
//         die();
        return new ViewModel(array(
//            'type'=>$data['type'],
            'user'=>$crmUser,
            'organization'=>$organizationList,
            'groupList'=>$groupList,
        ));
    }
    
    public function deleteUserAction()
    {
        $request = $this->getRequest();
        $user_id = $request->getPost('user_id');
        try{
            $this->getCrmUserTable()->deleteUser($user_id);
            $response=array(
                'result' => 'true',
                'reason' => '删除成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $response=array(
                'result' => 'false',
                'reason' => '系统错误！',
            );
            die(json_encode($response));
        }
    }
    /*
     * 用户列表
     */
    public function ajaxCrmuserListAction()
    {
        $post = $this->request->getPost()->toArray();
        
        $paginator = $this->getCrmUserTable()->getUserList($post);
        //var_dump($paginator);
        $result = array(
            'paginator' => $paginator,
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
        ->setTerminal(true);
        
        return $viewModel;
        
    }
    
    public function insertUserAction()
    {
        $crmUserTable = $this->getCrmUserTable();
        $data = $this->request->getPost()->toArray();
        $crmUser=array(
            'user_name'=>$data['user_name'],
            'user_group_id'=>$data["user_group_id"],
            'user_position'=>$data["user_position"],
            'org_id'=>$data["org_id"],
        );
        try{
            $crmUser['user_password']= md5(md5($data['user_password']));
            $crmUser['user_account']=$data['user_account'];
            $crmUserTable->addItem($crmUser);
            
            $this->flashMessenger()->addSuccessMessage("用户新增成功！");
            return $this->redirect()->toRoute('crm/crmuser');
        }catch(\Exception $e)
        {
            die($e);
            $this->flashMessenger()->addErrorMessage("用户新增失败！");
            return $this->redirect()->toRoute('crm/crmuser');
        }
    }
    public function updateUserAction()
    {
        $crmUserTable = $this->getCrmUserTable();
        $data = $this->request->getPost()->toArray();
        $crmUser=array(
            'user_name'=>$data['user_name'],
            'user_group_id'=>$data["user_group_id"],
            'org_id'=>$data["org_id"],
            'user_position'=>$data["user_position"],
        );
        try{
                if($data['user_password'] != $data['user_password_old']){
                    $crmUser['user_password']=md5(md5($data['user_password']));
                }
                $crmUserTable->updateItem($data['user_id'],$crmUser);
            
            $this->flashMessenger()->addSuccessMessage("用户修改成功！");
            return $this->redirect()->toRoute('crm/crmuser');
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("用户修改失败！");
            return $this->redirect()->toRoute('crm/crmuser');
        }
    }
    public function groupAction()
    {
        return new ViewModel();
    }
    public function addGroupAction()
    {
        $powerList = array();
        $powerList = $this->getPowerManageTable()->findAll();
//         var_dump($powerList);
//         die();
        return new ViewModel(array(
            'powerList'=>$powerList,
        ));
    }
    public function editGroupAction()
    {
        $data = $this->request->getQuery();
        $powerList = array();
        $powerList = $this->getPowerManageTable()->getGroupPower($data['id']);
        $group = $this->getUserGroupTable()->findOne($data['id']);
        return new ViewModel(array(
            'group'=>$group,
            'powerList'=>$powerList,
        ));
    }
    
    public function deleteGroupAction()
    {
        $request = $this->getRequest();
        $group_id = $request->getPost('group_id');
        try{
            $this->getUserGroupTable()->deleteItem($group_id);
            $response=array(
                'result' => 'true',
                'reason' => '删除成功！',
            );
            die(json_encode($response));
        }catch (\Exception $e){
            $response=array(
                'result' => 'false',
                'reason' => '系统错误！',
            );
            die(json_encode($response));
        }
    }
    /*
     * 用户组列表
     */
    public function ajaxGroupListAction()
    {
        $post = $this->request->getPost()->toArray();
        
        $paginator = $this->getUserGroupTable()->getGroupList($post);
        //var_dump($paginator);
        $result = array(
            'paginator' => $paginator,
        );
        
        $viewModel = new ViewModel();
        $viewModel->setVariables($result)
        ->setTerminal(true);
        
        return $viewModel;
        
    }
    public function insertGroupAction()
    {
        $crmUserTable = $this->getCrmUserTable();
        $data = $this->request->getPost()->toArray();
        try{
            $this->getUserGroupTable()->beginTransaction();
            $user_group_id = $this->getUserGroupTable()->addItem(array('user_group_name'=>$data['group_name']));
            foreach ($data as $key => $value){
                if(strpos($key, 'power_check_') !== false){
                    $this->getPowerGroupTable()->addItem(array('user_group_id'=>$user_group_id,'power_id'=>$value));
                }
            }
            $this->getUserGroupTable()->commit();
            $this->flashMessenger()->addSuccessMessage("用户组新增成功！");
            return $this->redirect()->toRoute('crm/crmuser',array('action'=>'group'));
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("用户组新增失败！");
            return $this->redirect()->toRoute('crm/crmuser',array('action'=>'group'));
        }
    }
    public function updateGroupAction()
    {
        $crmUserTable = $this->getCrmUserTable();
        $data = $this->request->getPost()->toArray();
        try{
            $this->getUserGroupTable()->beginTransaction();
            $this->getUserGroupTable()->updateItem($data['group_id'],array('user_group_name'=>$data['group_name']));
            $this->getPowerGroupTable()->deleteByGroupId($data['group_id']);
            foreach ($data as $key => $value){
                if(strpos($key, 'power_check_') !== false){
                    $this->getPowerGroupTable()->addItem(array('user_group_id'=>$data['group_id'],'power_id'=>$value));
                }
            }
            $this->getUserGroupTable()->commit();
    
            $this->flashMessenger()->addSuccessMessage("用户组修改成功！");
            return $this->redirect()->toRoute('crm/crmuser',array('action'=>'group'));
        }catch(\Exception $e)
        {
            //die($e);
            $this->flashMessenger()->addErrorMessage("用户组修改失败！");
            return $this->redirect()->toRoute('crm/crmuser',array('action'=>'group'));
        }
    }

}